import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

#class HelloHandler(webapp.RequestHandler):
#  def get(self):
#	DICE_ROLLS = self.request.get('DICE_ROLLS')
#	self.response.out.write('Hello ' + DICE_ROLLS)
#	self.response.out.write("<br><a href='/static/index.html'>My index</a>")
#	import random

class HelloHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'hello.html')  
    self.response.out.write(template.render(path, template_values))

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'name' : self.request.get('name') }
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class WrongAnswer(object):
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct

class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    addition_guess = self.request.get('addition').strip()
    template_values = {'wrong_answers': [], 'name':self.request.get('name')}
    if addition_guess != "7":
      if addition_guess == "":
        path = os.path.join(os.path.dirname(__file__), 'quiz.html')
        self.response.out.write(template.render(path, template_values))
        return
      else:
        template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))
    capital_guess = self.request.get('capital').strip().lower()
    if capital_guess != "sacramento":
      if addition_guess == "":
        path = os.path.join(os.path.dirname(__file__), 'quiz.html')
        self.response.out.write(template.render(path, template_values))
        return
      else:
        template_values['wrong_answers'].append(WrongAnswer("Capital", capital_guess, 'Sacramento'))

    path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
    self.response.out.write(template.render(path, template_values))

#    self.response.out.write("<br>Rolling the dice " + str(DICE_ROLLS) + " times<br>")
    return#this is nulling the the chunk below.
    for roll_num in range(int(DICE_ROLLS)):
        # Simulate rolling two dice.
        dice_1 = random.randint(1, 6)
        dice_2 = random.randint(1, 6)
        self.response.out.write("Rolls: %s %s<br>" % (dice_1, dice_2))
        if dice_1 == 1 and dice_2 == 1:
            self.response.out.write("You got snake eyes")
        if dice_1 == 6 and dice_2 == 6:
            self.response.out.write("double double in-n-out sucks")
class ByeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Bye app engine world!')

def main():
  application = webapp.WSGIApplication([('/hello.*', HelloHandler),
                                       ('/goodbye.*', ByeHandler),
                                       ('/quiz', QuizHandler),
                                       ('/grade_quiz', GradeQuizHandler)],
										debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

